<?php

namespace Admin\Controller;

use Think\Controller;

class GoodsController extends AdminController
{

    public function index()
    {
        $name = I('name');
        if (is_numeric($name)) {
            $map['id|name'] = array(intval($name), array('like', '%' . $name . '%'), '_multi' => true);
        } else {
            $map['name'] = array('like', '%' . (string) $name . '%');
        }

        $list = $this->lists('Goods', $map);
        $aa   = M('category')->getField('id,title');
        int_to_string($list);
        $this->assign('_list', $list);
        $this->assign('aa', $aa);
        $this->meta_title = '商品信息';
        $this->display();

    }

    public function add()
    {
        $goods = D('Goods');
        if (IS_POST) {
            $_POST['pic'] = $this->Upload('pic');
            $data         = $goods->create();
            if ($data) {
                $goods->add($data);
                $this->success('添加成功', U('index'));
            } else {
                $this->error($goods->getError());
            }

        } else {
            $res = M('category')->where(['pid' => 0])->select();
            $this->assign('res', $res);
            $this->display();
        }
    }

    public function ajaxxiala()
    {
        $pid = I('get.pid');
        $res = M('category')->where(['pid' => $pid])->select();
        if ($res) {
            $this->assign('res', $res);
            $data['res']    = $this->fetch('ajaxxiala');
            $data['status'] = 1;
            $data['info']   = '发送成功';
        } else {
            $data['res']    = null;
            $data['status'] = 0;
            $data['info']   = '没有需要查询的数据';
        }
        $this->ajaxReturn($data);
    }

    public function update()
    {
        $goods = D('Goods');

        if (IS_GET) {
            $dd = D('category')->where(['pid' => 0])->select();
            $this->assign('dd', $dd);
            $res = $goods->find(I('get.id'));
            $this->assign('res', $res);
            $this->display();
        } else {
            if ($_FILES['pic']['error'] != 4) {
                $_POST['pic'] = $this->upload('pic');
            }

            $data = $goods->create();
            if ($data) {
                $goods->where(['id' => I('post.id')])->save($data);
                $this->success('修改成功', U('index'));

            } else {
                $this->error($goods->getError());
            }

        }

    }
    public function upload($inputName)
    {
        $upload           = new \Think\Upload(); // 实例化上传类
        $upload->maxSize  = 3145728; // 设置附件上传大小
        $upload->exts     = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
        $upload->rootPath = './Uploads/'; // 设置附件上传根目录
        // 上传单个文件
        $info = $upload->uploadOne($_FILES[$inputName]);
        if (!$info) {
            // 上传错误提示错误信息
            $this->error($upload->getError());
        } else {
            // 上传成功 获取上传文件信息
            return $info['savepath'] . $info['savename'];
        }
    }
    public function changeStatus($method = null)
    {
        $id = array_unique((array) I('id', 0));
        $id = is_array($id) ? implode(',', $id) : $id;
        if (empty($id)) {
            $this->error('请选择要操作的数据!');
        }
        $map['id'] = array('in', $id);
        switch (strtolower($method)) {
            case 'hide':
                $this->forbid('Goods', $map);
                break;
            case 'show':
                $this->resume('Goods', $map);
                break;
            case 'del':
                D('Goods')->where($map)->delete();
                $this->success('删除成功');
                break;
            default:
                $this->error('参数非法');
        }
    }

    public function attrkadd()
    {
        if (IS_GET) {
            $this->display();
        } else {
            $data = I('post.');
            $res  = M('attrk')->add($data);
            if ($res) {
                $this->success('成功');
            } else {
                $this->error('失败');
            }
        }
    }
    public function attr()
    {
        if (IS_GET) {
            $aa = I('get.id');
            //$res  = M('attrk')->where(['gid' => $aa])->field('id')->select();

            // $arr1 = array();
            // foreach ($res as $key => $value) {
            //     foreach ($value as $k => $v) {
            //         $arr1[] = $v;
            //     }
            // }
            // $k = count($arr1);
            // for ($i = 0; $i < $k; $i++) {
            //     $sb = 'arr';
            //     $sb .= $i;
            //     $a    = $arr1[$i];
            //     $sb   = M('attrv')->where(['kid' => $a])->field('id')->select();
            //     $xx[] = $sb;
            // }
            // $data1 = $this->dikaer($xx);
            // $zz    = [];
            // foreach ($data1 as $value) {
            //     foreach ($value as $v) {
            //         $zz[] = $v;
            // }
            //}
            //var_dump($zz);
            //$bb = M('attrv')->getField('id,name');
            //$this->assign('bb', $bb);
            $res = M('attrv')->where(['gid' => $aa])->select();
            foreach ($res as $v) {
                $arr[$v['kid']][] = $v['name'];
            }
            $data = array_values($arr);
            $zz   = $this->dikaer($data);
            $cc   = [];
            foreach ($zz as $v) {
                sort($v);
                $xx   = implode(',', $v);
                $cc[] = $xx;
            }
            $this->assign('gid', $aa);
            $this->assign('arr', $cc);
            $this->display();
        } else {
            $res = I('post.');
            $arr = [];
            foreach ($res['num'] as $k => $v) {
                $map            = [];
                $map['gid']     = $res['gid'];
                $map['price']   = $res['price'][$k];
                $map['num']     = $res['num'][$k];
                $map['attrvid'] = $res['attrvid'][$k];
                $arr[]          = $map;
            }
            $data = M('attr')->addAll($arr);
            if ($data) {
                $this->success('成功');
            } else {
                $this->error('失败');
            }
        }
    }
    public function attrindex()
    {
        $res   = M('attr')->select();
        $jl    = M('attr')->field('attrvid')->select();
        $goods = M('goods')->getField('id,name');
        $attrv = M('attrv')->getField('id,name');
        //dump($jl);
        $arr = [];
        foreach ($jl as $k => $v) {
            $arr[] = explode(',', $v['attrvid']);
        }

        $this->assign('_list', $res);
        $this->assign('attrv', $attrv);
        $this->assign('goods', $goods);
        $this->display();
    }
    public function attrupdate()
    {
        if (IS_GET) {
            $id    = I('get.id');
            $res   = M('attr')->where(['id' => $id])->find();
            $goods = M('goods')->getField('id,name');
            $this->assign('res', $res);
            $this->assign('goods', $goods);
            $this->display();
        } else {
            $id   = I('post.id');
            $data = I('post.');
            $res  = M('attr')->where(['id' => $id])->save($data);
            if ($res) {
                $this->success('修改成功', U('attrindex'));
            } else {
                $this->error('修改失败');
            }

        }
    }
    public function attrdel()
    {
        $id  = I('get.id');
        $res = M('attr')->delete($id);
        if ($res) {
            $this->success('删除成功', U('attrindex'));
        } else {
            $this->error('删除失败');
        }

    }

    public function dikaer($arr)
    {
        $arr1   = array();
        $result = array_shift($arr);
        while ($arr2 = array_shift($arr)) {
            $arr1   = $result;
            $result = array();
            foreach ($arr1 as $v) {
                foreach ($arr2 as $v2) {
                    if (!is_array($v)) {
                        $v = array($v);
                    }

                    if (!is_array($v2)) {
                        $v2 = array($v2);
                    }

                    $result[] = array_merge_recursive($v, $v2);
                }
            }
        }
        return $result;
    }

}
